package com.topnav.mc.admin.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.js.lib.utils.JsonUtil;
import com.topnav.mc.admin.dao.mapper.OperateLogMapper;
import com.topnav.mc.admin.entity.OperateLog;
import com.topnav.mc.admin.entity.bean.OperateLogBean;
import com.topnav.mc.admin.entity.enums.EnumLogType;
import com.topnav.mc.admin.entity.enums.EnumOperateFunction;
import com.topnav.mc.admin.entity.param.OperateLogParam;
import com.topnav.mc.admin.security.PermissionGrade;
import com.topnav.mc.admin.security.jwt.utils.JwtTokenUtil;
import com.topnav.mc.admin.service.IOperateLogService;
import com.topnav.mc.base.mc.McServiceImpl;
import com.topnav.mc.common.Constant;
import com.topnav.mc.uitls.PermissionGradeUtil;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @Author: kenny
 * @Date: 2024/3/29 17:22
 */
@Service
public class OperateLogServiceImpl extends McServiceImpl<OperateLogMapper, OperateLog, OperateLogBean, OperateLogParam> implements IOperateLogService {
    @Resource
    private PermissionGradeUtil permissionGradeUtil;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public boolean beforeSave(OperateLog entity){
        entity.setTime(new Date());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return true;
    }

    @Override
    public boolean save(OperateLog entity) {
        if (!beforeSave(entity)){return false;}
        //如果登录日志则直接保存
        if(entity.getLogType() != EnumLogType.LOGIN.getCode()){
            String token = MDC.get(Constant.MDC_TOEKN);
            if (StringUtils.isEmpty(token)){
                return false;
            }else{
                String clientIp = jwtTokenUtil.getClientIpFromToken(token);
                PermissionGrade permissionGrade = permissionGradeUtil.getPmGradeCheck();
                if ("super".equals(permissionGrade.getUserName())){
                    return true;
                }
                entity.setClientIp(clientIp);
                entity.setUserId(permissionGrade.getUserId());
                entity.setCreateBy(permissionGrade.getUserName());
            }
        }
        return super.save(entity);
    }

    @Override
    public boolean saveLog(EnumLogType type, EnumOperateFunction function, String context){
        OperateLog operateLog = new OperateLog();
        operateLog.setLogType(type.getCode());
        operateLog.setFunctionId(function.getCode());
        if (!StringUtils.isEmpty(context))
            operateLog.setContext(context);
        return this.save(operateLog);
    }

    @Override
    public boolean saveLog(EnumLogType type, EnumOperateFunction function, Map<String,Object> context){
        return this.saveLog(type,function, JsonUtil.toJson(context));
    }

    @Override
    public boolean saveLog(EnumLogType type, EnumOperateFunction function, JSONObject context){
        return this.saveLog(type,function, context.toString());
    }

    @Override
    public boolean saveLog(EnumLogType type, EnumOperateFunction function, Object context){
        return this.saveLog(type,function, JsonUtil.toJson(context));
    }

    @Override
    public boolean saveLog(EnumLogType type, EnumOperateFunction function){
       return saveLog(type,function,"");
    }

}
